<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入查询关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getDictionarys">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!-- 列表 -->
    <el-table :data="dictionarys" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" :visible="drawer" @row-dblclick="rowDrawerClick">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="180" sortable >
      </el-table-column>
      <el-table-column prop="sn" label="产品编号" width="180" sortable>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="dictionaryFormVisible" :close-on-click-modal="false">
      <el-form :model="dictionaryForm" label-width="100px" :rules="dictionaryFormRules" ref="dictionaryForm">
        <el-form-item label="数据字典名"  prop="name">
          <el-input v-model="dictionaryForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品编号" prop="sn">
          <el-input v-model="dictionaryForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="dictionaryFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="dictionarySubmit" :loading="dictionaryLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :currentPage="page"
                     :pageSize="pageSize"
                     :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>
    <!--内层抽屉列表展示-->

    <!-- 抽屉 -->
    <el-drawer
        :visible.sync="drawer"
        :with-header="false">
      <!-- 列表 -->
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
          <el-form-item>
            <el-input v-model="filters.keyword" placeholder="请输入查询关键字"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="getDictionarys">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAdd1">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <!-- 列表 -->
      <el-table :data="types" highlight-current-row v-loading="listLoading" @selection-change="selsChange1" style="width: 100%;" :visible="drawer" @row-dblclick="rowDrawerClick(scope.row)">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="数据字典明细" width="180"   >
        </el-table-column>
        <el-table-column label="操作" >
          <template scope="scope">
            <el-button size="small" @click="handleEdit1(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel1(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--编辑界面-->
      <el-dialog title="" :visible.sync="typeFormVisible" :close-on-click-modal="false" append-to-body = true>
        <el-form :model="typeForm" label-width="100px" :rules="typeFormRules" ref="typeForm">
          <el-form-item label="数据字典明细"  prop="name">
            <el-input v-model="typeForm.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="字典明细索引"  prop="types_id">
            <el-input v-model="typeForm.types_id" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="typeFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="typeSubmit" :loading="typeLoading">提交</el-button>
        </div>
      </el-dialog>
      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove1" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next"
                       @current-change="handleCurrentChange1"
                       :current-page="page"
                       :pageSize="pageSize"
                       :total="total"
                       style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>
  </section>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        title: ' ',
        filters: {
          keyword: ' '
        },
      },
      types_id:null,
      //默认不显示抽屉
      drawer: false,
      //多层抽屉
      innerDrawer: true,
      //添加、编辑动态标题绑定
      //是否打开组件
      dialogVisible: false,
      //添加、编辑公共前缀绑定
      dictionarys: [],
      types:[],
      employees: [],
      deptTree:[],
      typeTree:[],
      total: 0,
      pageSize: 5,
      page: 1,
      listLoading: false,
      sels: [],// 列表选中列
      wrapperClosable:true,

      typeFormVisible:false,
      typeLoading: false,
      dictionaryFormVisible: false,// 编辑界面是否显示
      dictionaryLoading: false,
      dictionaryFormRules: {// 申请表单属性的校验规则
        name: [
          {required: true, message: '数据字典名', trigger: 'blur'}
        ],
        sn: [
          {required: true, message: '产品编号', trigger: 'blur'}
        ]
      },
      typeFormRules: {// 申请表单属性的校验规则
        name: [
          {required: true, message: '数据字典明细', trigger: 'blur'}
        ],
      },
      // 部门表单界面数据
      typeForm: {
        id: null,
        name: '',
        types_id:""
      },
      dictionaryForm: {
        id: null,
        name: '',
        sn: '',
      },
    }
  },
  methods: {
    handleCurrentChange1(val) {
      this.page = val;
      this.getTypes()
    },
    // 获取明细列表
    getTypes(val) {
      let para = {
        currentPage: this.page,
        keyword: this.filters.keyword,
        pageSize: this.pageSize, // 每页条数
        types_id:val
      };
      this.listLoading = true;// 开启忙等框
      this.$http.post("/dic", para)
          .then(result => {
            result = result.data;//PageList{total rows}
            this.types = result.rows;
            console.debug(this.types);
            this.listLoading = false;// 关闭忙等框
            this.total = result.total;
          })
          .catch(result => {
            console.log(result);
          })
    },
    // 删除
    handleDel1: function (index, row) {
      this.$confirm('请确认你是否为手贱误点?', '祖安提示', {
        type: 'warning'
      }).then(() => { // 点击确定
        this.listLoading = true;
        //NProgress.start();
        this.$http.delete("/dic/" + row.id)
            .then(result => {
              this.listLoading = false;  // 关闭忙等框
              result = result.data;  // 操作失败
              if (result.success) {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: result.message, // 自己的业务出错
                  type: "error",
                });
              }
              // 刷新页面并定位到第一页
              this.page= 1;
              this.getTypes(this.types_id);
            })
      }).catch(result => {
        this.listLoading = false;  // 关闭忙等框
        this.$message({
          message: "操作异常，已滚出",
          type: "error"
        });
        // 操作失败刷新页面并定位到第一页
        this.page = 1;
        this.getTypes(this.types_id);
      }).catch(() => {  // 点击取消

      });
    },
    // 显示编辑界面
    handleEdit1: function (index, row) {
      this.getTypeTree();
      this.title = "编辑";
      this.typeFormVisible = true;
      this.typeForm = Object.assign({}, row);// 克隆回显
      let dirPath = row.dirPath;
      if(dirPath){
        let array = dirPath.split("/");
        let parent = [];
        for(let i = 1; i < array.length - 1; i++){
          parent.push(parseInt(array[i]));
        }
        console.log(parent);
        this.typeForm.parent = parent;
      }
    },
    // 显示新增界面
    handleAdd1: function () {
      this.getTypeTree(this.types_id);
      this.title = "新增";
      this.typeFormVisible = true;
      this.typeForm = {  // 清空历史数据
        id:null,
        name: '',
      };
    },
    // 提交表单
    typeSubmit: function () {
      this.$refs.typeForm.validate((valid) => {  // 开启表单检验s
        if (valid) {  // 校验通过
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.typeLoading = true;// 开启忙等框
            let para = Object.assign({}, this.typeForm);// 克隆表单数据
            // 处理后台需要的数据格式
            if(para.parent){
              let parent = para.parent;
              // 获取数组的最后一个
              let pid = parent[parent.length-1];
              // 重新设置parent的值
              para.parent = {"id":pid};
            }

            this.$http.put("/dic",para)
                .then(result=>{
                  result = result.data;//AjaxResult
                  this.typeLoading = false;// 关闭忙等框
                  this.typeFormVisible = false;// 关闭弹出框
                  if(result.success){
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                  }else{
                    this.$message({
                      message: result.message,// 提示业务出错
                      type: 'error'
                    });
                  }
                  // 刷新页面并定位到第一页
                  this.page = 1;
                  this.getTypes(this.types_id);

                })
                .catch(result=>{
                  this.typeLoading = false;// 关闭忙等框
                  this.$message({
                    message: '操作异常，已滚出',
                    type: 'error'
                  });
                })
          });
        }
      });
    },
    selsChange1: function (sels) {// 当前已经选中的所有行的数据
      this.sels = sels;
    },
    //批量删除
    batchRemove1: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中的记录吗？', '祖安提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.patch("/dic", ids)
            .then(result => {
              this.listLoading = false;
              // 判断业务是否正常
              result = result.data;
              if (result.success) {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: result.message,  // 提示代码错误
                  type: "error"
                });
              }
              // 刷新页面并定位到第一页
              this.page=1;
              this.getTypes(this.types_id);
            })
      }).catch(() => {
        this.listLoading = false;  // 关闭忙等框
        this.$message({
          message: "操作异常，已滚出",
          type: "error"
        });
        this.page = 1;
        this.getTypes(this.types_id);
      }).catch(() => {

      });
    },
    // ====================================外
    //双击显示内层抽屉
    rowDrawerClick(row) {
      //弹出内层抽屉
      this.drawer = true,
      this.innerDrawer = true,
          console.log(row.id);
      this.getTypes(row.id);
      this.types_id = row.id;

    },
    // 获取状态
    handleCurrentChange(val) {
      this.page = val;
      this.getDictionarys();
    },
    // 获取字典列表
    getDictionarys() {
      let para = {
        currentPage: this.page,
        page:this.currentPage,
        keyword: this.filters.keyword,
        pageSize: this.pageSize // 每页条数
      };
      this.listLoading = true;// 开启忙等框
      this.$http.post("/type", para)
          .then(result => {
            result = result.data;//PageList{total rows}
            this.dictionarys = result.rows;
            this.listLoading = false;// 关闭忙等框
            this.total = result.total;
          })
          .catch(result => {
            console.log(result);
            alert("操作异常，已滚出！");
          })
    },
    // 删除
    handleDel: function (index, row) {
      this.$confirm('请确认你是否为手贱误点?', '祖安提示', {
        type: 'warning'
      }).then(() => { // 点击确定
        this.listLoading = true;
        //NProgress.start();
        this.$http.delete("/type/" + row.id)
            .then(result => {
              this.listLoading = false;  // 关闭忙等框
              result = result.data;  // 操作失败
              if (result.success) {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: result.message, // 自己的业务出错
                  type: "error",
                });
              }
              // 刷新页面并定位到第一页
              this.page= 1;
              this.getDictionarys();
            })
      }).catch(result => {
        this.listLoading = false;  // 关闭忙等框
        this.$message({
          message: "操作异常，已滚出",
          type: "error"
        });
        // 操作失败刷新页面并定位到第一页
        this.page = 1;
        this.getDictionarys();
      }).catch(() => {  // 点击取消

      });
    },
    // 显示编辑界面
    handleEdit: function (index, row) {
      this.getDeptTree();
      this.title = "编辑";
      this.dictionaryFormVisible = true;
      this.dictionaryForm = Object.assign({}, row);// 克隆回显
      let dirPath = row.dirPath;
      if(dirPath){
        let array = dirPath.split("/");
        let parent = [];
        for(let i = 1; i < array.length - 1; i++){
          parent.push(parseInt(array[i]));
        }
        console.log(parent);
        this.dictionaryForm.parent = parent;
      }
    },
    // 显示新增界面
    handleAdd: function () {
      this.getDeptTree();
      this.title = "新增";
      this.dictionaryFormVisible = true;
      this.dictionaryForm = {  // 清空历史数据
        id: null,
        name: '',
        sn: '',
      };
    },
    // 提交表单
    dictionarySubmit: function () {
      this.$refs.dictionaryForm.validate((valid) => {  // 开启表单检验
        if (valid) {  // 校验通过
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.dictionaryLoading = true;// 开启忙等框
            let para = Object.assign({}, this.dictionaryForm);// 克隆表单数据
            // 处理后台需要的数据格式
            if(para.parent){
              let parent = para.parent;
              // 获取数组的最后一个
              let pid = parent[parent.length-1];
              // 重新设置parent的值
              para.parent = {"id":pid};
            }

            this.$http.put("/type",para)
                .then(result=>{
                  result = result.data;//AjaxResult
                  this.dictionaryLoading = false;// 关闭忙等框
                  this.dictionaryFormVisible = false;// 关闭弹出框
                  if(result.success){
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                  }else{
                    this.$message({
                      message: result.message,// 提示业务出错
                      type: 'error'
                    });
                  }
                  // 刷新页面并定位到第一页
                  this.page = 1;
                  this.getDictionarys();

                })
                .catch(result=>{
                  this.dictionaryLoading = false;// 关闭忙等框
                  this.$message({
                    message: '操作异常，已滚出',
                    type: 'error'
                  });
                })
          });
        }
      });
    },
    selsChange: function (sels) {// 当前已经选中的所有行的数据
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中的记录吗？', '祖安提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.patch("/type", ids)
            .then(result => {
              this.listLoading = false;
              // 判断业务是否正常
              result = result.data;
              if (result.success) {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: result.message,  // 提示代码错误
                  type: "error"
                });
              }
              // 刷新页面并定位到第一页
              this.page=1;
              this.getDictionarys();
            })
      }).catch(() => {
        this.listLoading = false;  // 关闭忙等框
        this.$message({
          message: "操作异常，已滚出",
          type: "error"
        });
        this.page = 1;
        this.getDictionarys();
      }).catch(() => {

      });
    },
    getDeptTree(){
      this.$http.get("/type/tree")
          .then(result=>{
            result = result.data;
            this.deptTree  = result;
          })
    },
    getTypeTree(){
      this.$http.get("/dic/tree")
          .then(result=>{
            result = result.data;
            this.deptTree  = result;
          })
    }
  },
  mounted() {
    this.getDictionarys();
  }
}
</script>

<style scoped>


</style>